﻿using SearchAlgorithms.Interfaces;
using System;
using System.Linq;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Security.Cryptography;

namespace SearchAlgorithms.Algorithms.Compare
{
    public class CompareWithMethod : ICompareAlgorithm
    {
        /// <summary>
        /// 比较两个字节数组是否完全相同。
        /// </summary>
        /// <param name="frameA">要比较的第一个字节数组。</param>
        /// <param name="frameB">要比较的第二个字节数组。</param>
        /// <returns>如果两个数组完全相同，返回 true；否则返回 false。</returns>
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public bool Comparison(byte[] frameA, byte[] frameB)
        {
            if (frameA.Length != frameB.Length)
            {
                return false;
            }

            for (int i = 0; i < frameA.Length; i++)
            {
                if (frameA[i] != frameB[i])
                {
                    return false;
                }
            }
            return true;
        }

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public bool Comparison(Span<byte> frameA, Span<byte> frameB)
        {
            if (frameA.Length != frameB.Length)
            {
                return false;
            }

            for (int i = 0; i < frameA.Length; i++)
            {
                if (frameA[i] != frameB[i])
                {
                    return false;
                }
            }
            return true;
        }

        

        /// <summary>
        /// LINQ语句比较两个字节数组是否按顺序完全相同。
        /// </summary>
        /// <param name="src">要比较的源字节数组。</param>
        /// <param name="dst">要比较的目标字节数组。</param>
        /// <returns>如果两个数组长度相同且每个对应位置上的字节都相同，返回 true；否则返回 false。</returns>
        public bool CompareWithSequence(byte[] src, byte[] dst)
        {
            return src.SequenceEqual(dst);
        }

        public bool CompareWithSequence(Span<byte> src, Span<byte> dst)
        {
            return src.SequenceEqual(dst);
        }

        public bool CompareWithHash(byte[] src, byte[] dst, HashAlgorithm hash)
        {
            byte[] hashSrc = hash.ComputeHash(src);
            byte[] hashDst = hash.ComputeHash(dst);
            
            return hashSrc.SequenceEqual(hashDst);
        }

        public bool CompareWithHashAsSpan (byte[] src, byte[] dst, HashAlgorithm hash)
        {
            // 计算源和目标的哈希值
            Span<byte> HashSrc = hash.ComputeHash(src);
            Span<byte> HashDst = hash.ComputeHash(dst);

            // 比较两个哈希值
            return HashSrc.SequenceEqual(HashDst);
        }
    }
}
